package io;

import java.util.Vector;

import utils.SaveLoad;

public class MotifReadWrite {

	public static String MOTIFFILE = "motif.txt";
	public static String MOTIFFILELEN = "motiflength.txt";
	public static int motifNum = 0;
	
	/**
	 * Wrapper for saveFile.
	 * @param motifMatrix
	 * @param motifLen
	 * @param filename
	 */
	public static void saveFile(Vector<Vector<Double>> motifMatrix, int motifLen, String filename)
	{
		MOTIFFILE = filename;
		saveFile(motifMatrix, motifLen);
	}
	/**
	 * motifNum | motifLength
	 * @param motifMatrix
	 * @param motifLen
	 */
	public static void saveFile(Vector<Vector<Double>> motifMatrix, int motifLen)
	{
		SaveLoad.saveFile(MOTIFFILE, ">" + motifNum++ + "|" + motifLen + "\n");
		for (int i = 0; i < motifMatrix.size(); i++)
		{
			String currLine = "";
			for (int inner = 0; inner < motifMatrix.get(i).size(); inner++)
			{
				currLine += "" + motifMatrix.get(i).get(inner) + " ";
			}
			if (!currLine.isEmpty())
				SaveLoad.saveFile(MOTIFFILE, currLine.substring(0, currLine.length() - 2) + "\n");
			SaveLoad.saveFile(MOTIFFILELEN, "" + motifLen + "\n");
		}
	}
	
	/**
	 * Loads how long the motif len is
	 * @return
	 */
	public static int loadMotifLen()
	{
		return Integer.parseInt(SaveLoad.loadFile(MOTIFFILELEN));
	}
	
	/**
	 * Doesn't work!!!
	 * @param motifLen
	 * @return
	 */
	public static double [] loadFile(int motifLen)
	{
		double [] motifMatrix = new double[4 * motifLen];
		String content = SaveLoad.loadFile(MOTIFFILE);
		String [] contentSplitted = content.split("\n");
		for (int i = 0; i < 4; i++)
		{
			String [] currSplitted = contentSplitted[i].split(" ");
			for (int c = 0; c < motifLen; c++)
			{
				double currVal = Double.parseDouble(currSplitted[c]);
				motifMatrix[i * motifLen + c] = currVal;
			}
		}
		
		return motifMatrix;
	}
}
